package com.yjt.javatest.utils;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Expression;
import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.function.FunctionUtils;
import com.googlecode.aviator.runtime.type.AviatorBigInt;
import com.googlecode.aviator.runtime.type.AviatorDouble;
import com.googlecode.aviator.runtime.type.AviatorObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fengye
 * @Date: 2022-09-16-10:22
 * @Description:
 */
public class Aviator {
    //需实例化该函数，以便调用里面的函数
    public Aviator() {
        AviatorEvaluator.addFunction(new MinFunction());
        AviatorEvaluator.addFunction(new acos());
        AviatorEvaluator.addFunction(new asin());
        AviatorEvaluator.addFunction(new atan());
        AviatorEvaluator.addFunction(new pi());
        AviatorEvaluator.addFunction(new e());
    }

    public static void main(String[] args) {
        new Aviator();
        try{
        String expression = "math.sqrt(math.pow((-3*0.01),2)+math.pow(v0,2)/math.pow(w,2))";
        Expression compiledExp = AviatorEvaluator.compile(expression, true);
        Map<String, Object> env = new HashMap<>();
        env.put("x0",-3);
        env.put("v0",-0.3);
        env.put("w",10.0);
            System.out.println(env);
            Object result = compiledExp.execute(env);
        System.out.println(result);
        }
        catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
//        AviatorEvaluator.execute()
    }

    static class MinFunction extends AbstractFunction {
        @Override public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2) {
            Number left = FunctionUtils.getNumberValue(arg1, env);
            Number right = FunctionUtils.getNumberValue(arg2, env);
            return new AviatorBigInt(Math.min(left.doubleValue(), right.doubleValue()));
        }

        @Override
        public String getName() {
            return "min";
        }
    }
    static class acos extends AbstractFunction {
        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1) {
            Number left = FunctionUtils.getNumberValue(arg1, env);
            return new AviatorDouble(Math.acos(left.doubleValue()));
        }

        @Override
        public String getName() {
            return "acos";
        }
    }
    static class asin extends AbstractFunction {
        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1) {
            Number left = FunctionUtils.getNumberValue(arg1, env);
            return new AviatorDouble(Math.asin(left.doubleValue()));
        }

        @Override
        public String getName() {
            return "asin";
        }
    }
    static class atan extends AbstractFunction {
        @Override
        public AviatorObject call(Map<String, Object> env, AviatorObject arg1) {
            Number left = FunctionUtils.getNumberValue(arg1, env);
            return new AviatorDouble(Math.atan(left.doubleValue()));
        }

        @Override
        public String getName() {
            return "atan";
        }
    }
    static class pi extends AbstractFunction {
        @Override
        public AviatorObject call(Map<String, Object> env) {
            return new AviatorDouble(3.14);
        }

        @Override
        public String getName() {
            return "pi";
        }
    }
    static class e extends AbstractFunction {
        @Override
        public AviatorObject call(Map<String, Object> env) {
            return new AviatorDouble(3.14);
        }

        @Override
        public String getName() {
            return "e";
        }
    }
}
